package com.zhfi.dataapply.schedule.writer.chaincode;

import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.zhfi.dataapply.entity.data.po.chaincode.ChainInvestsPO;
import com.zhfi.dataapply.entity.data.po.chaincode.InvestEventPagePO;
import com.zhfi.dataapply.entity.data.po.chaincode.IpoDataPO;
import com.zhfi.dataapply.entity.data.vo.chaincode.InvestEventPageVO;
import com.zhfi.dataapply.mapper.chaincode.ChainInvestsMapper;
import com.zhfi.dataapply.mapper.chaincode.InvestEventPageMapper;
import com.zhfi.dataapply.mapper.chaincode.IpoDataMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.batch.item.ItemWriter;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;

import java.util.List;

@Slf4j
@Component
public class InvestEventPageWriter implements ItemWriter<InvestEventPageVO> {

    @Autowired
    private InvestEventPageMapper investEventPageMapper;

    @Autowired
    private IpoDataMapper ipoDataMapper;

    @Autowired
    private ChainInvestsMapper chainInvestsMapper;

    @Override
    public void write(List<? extends InvestEventPageVO> list) throws Exception {
        for (InvestEventPageVO vo : list) {
            try {
                InvestEventPagePO investEventPagePO = new InvestEventPagePO();
                BeanUtils.copyProperties(vo, investEventPagePO);
                UpdateWrapper<InvestEventPagePO> investEventPagePOUpdateWrapper = new UpdateWrapper<>();
                investEventPagePOUpdateWrapper.eq("event_id", investEventPagePO.getEventId());
                investEventPagePOUpdateWrapper.eq("chain_code", investEventPagePO.getChainCode());
                int update = investEventPageMapper.update(investEventPagePO, investEventPagePOUpdateWrapper);
                if (update <= 0) {
                    investEventPageMapper.insert(investEventPagePO);
                }
                List<IpoDataPO> ipoData = vo.getIpoData();
                if (!CollectionUtils.isEmpty(ipoData)) {
                    ipoData.forEach(po -> {
                        po.setCompanyId(vo.getCompanyId());
                        UpdateWrapper<IpoDataPO> updateWrapper = new UpdateWrapper<>();
                        updateWrapper.eq("company_id", po.getCompanyId());
                        updateWrapper.eq("ipo_type", po.getIpoType());
                        if (ipoDataMapper.update(po, updateWrapper) <= 0) {
                            ipoDataMapper.insert(po);
                        }
                    });
                }
                List<ChainInvestsPO> invests = vo.getInvests();
                if (!CollectionUtils.isEmpty(invests)) {
                    invests.forEach(po -> {
                        UpdateWrapper<ChainInvestsPO> updateWrapper = new UpdateWrapper<>();
                        updateWrapper.eq("event_id", po.getEventId());
                        updateWrapper.eq("invest_id", po.getInvestId());
                        if (chainInvestsMapper.update(po, updateWrapper) <= 0) {
                            chainInvestsMapper.insert(po);
                        }
                    });
                }
            } catch (Exception e) {
                log.error("InvestEventPageWriter写入数据失败：" + vo, e);
            }
        }
    }
}
